package com.rongshu.widget;


import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.view.discovery.adapter.SelectLabelAdapter;
import com.rongshu.view.home.bean.LabelResponse;

import java.util.ArrayList;
import java.util.List;

public class SelectLabelDialog extends Dialog {
    public SelectLabelDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private Context context; // 上下文对象
        private int mMainLayout = R.layout.dialog_select_label;
        private List<LabelResponse.ResponseBean> tagsList = new ArrayList<>();
        private SelectLabelAdapter adapter;
        private dialogClickListener listener;
        private TextView sure;

        public Builder(Context context) {
            this.context = context;
        }

        public interface dialogClickListener {
            void ClickListener(List<LabelResponse.ResponseBean> tagsList);
        }

        public Builder setLabels(List<LabelResponse.ResponseBean> tagsList) {
            this.tagsList.addAll(tagsList);
            adapter.notifyDataSetChanged();
            return this;
        }

        public void setDialogClickListener(dialogClickListener listener) {
            this.listener = listener;
        }

        public SelectLabelDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final SelectLabelDialog dialog = new SelectLabelDialog(context, R.style.CustomDialogStyle);
            View layout = inflater.inflate(mMainLayout, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            layout.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            sure = layout.findViewById(R.id.btn_sure);
            sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        listener.ClickListener(getSelectedLabel());
                    }
                    dialog.dismiss();
                }
            });
            RecyclerView labels = layout.findViewById(R.id.labels);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
            labels.setLayoutManager(gridLayoutManager);
            adapter = new SelectLabelAdapter(context, tagsList);
            labels.setAdapter(adapter);
            dialog.setContentView(layout);
            return dialog;
        }


        private List<LabelResponse.ResponseBean> getSelectedLabel() {
            List<LabelResponse.ResponseBean> selectedList = new ArrayList<>();
            for (LabelResponse.ResponseBean bean : tagsList) {
                if (bean.getIsSelected()) {
                    selectedList.add(bean);
                }
            }
            return selectedList;
        }

        public void removeSelect(LabelResponse.ResponseBean item) {
            for (LabelResponse.ResponseBean bean : tagsList) {
                if (bean.getId() == item.getId()){
                    bean.setIsSelected(false);
                }
            }
            adapter.notifyDataSetChanged();
        }

        public void resetLabel() {
            for (LabelResponse.ResponseBean bean : tagsList) {
                bean.setIsSelected(false);
            }
            adapter.notifyDataSetChanged();
        }

        public void syncLabel(List<LabelResponse.ResponseBean> select) {
            for (LabelResponse.ResponseBean bean : tagsList){
                for (LabelResponse.ResponseBean item : select){
                    if (bean.getId() == item.getId()){
                        bean.setIsSelected(true);
                    }
                }
            }
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams params = this.getWindow().getAttributes();
        params.width = (int) getContext().getResources().getDimension(R.dimen.dd_dimen_660px);
        params.height = (int) getContext().getResources().getDimension(R.dimen.dd_dimen_1060px);
        this.getWindow().setAttributes(params);
    }
}
